package mdm.led.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.BaseController;
import mdm.led.common.Constant;
import mdm.led.entity.Permission;
import mdm.led.entity.Role;
import mdm.led.entity.RolePermission;
import mdm.led.entity.User;
import mdm.led.log.OperationLog;
import mdm.led.service.IRoleService;
import mdm.led.service.UserService;


@Controller
@RequestMapping(value = "role")
@Api(description = Constant.MODULE_ROLE)
public class RoleController  extends BaseController{

    private static final Logger LOGGER = LoggerFactory.getLogger(RoleController.class);
    
    @Autowired
    private IRoleService iroleService;
    
    @Autowired
    private UserService userService;
    
    @ApiOperation(value = "权限详情列表", notes = "")
    @ResponseBody
    @RequestMapping(value = "/permission", method = RequestMethod.POST)
    public AjaxResponse getPermission() {
        try {
            List<Permission> list = iroleService.selectByPermission();
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, list);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }
    
    @ApiOperation(value = "查询列表", notes = "请求参数:<br>" + "pageIndex 页码<br>" + "limit 每页显示条数<br>")
    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public AjaxResponse page(@RequestBody Role role, @RequestParam Integer pageIndex, @RequestParam Integer limit) {
        try {
            LOGGER.info("------------role page select:{}---{}---{}", role, pageIndex, limit);
            
            PageHelper.startPage(pageIndex, limit);
            List<Role> list = iroleService.selectByCondition(role);
            PageInfo<Role> result = new PageInfo<>(list);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, result);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }
    
    @ApiOperation(value = "新增角色", notes = "")
    @ResponseBody
    @RequestMapping(value = "/addRole", method = RequestMethod.POST)
    public AjaxResponse add(@RequestBody Role role) {
        try {
        	if (StringUtils.isEmpty(role.getcRoleName())) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE, "角色名称为空！！！", null);
            }
            if (iroleService.selectByName(role) != null) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE, "角色名称已存在", null);
            }
            iroleService.insertSelective(role);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, null);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }
    
    
    @ApiOperation(value = "修改角色信息", notes = "")
    @ResponseBody
    @RequestMapping(value = "/modify", method = RequestMethod.PUT)
    public AjaxResponse modify(@RequestBody Role role) {
        try {
            LOGGER.info("------------param:{}", role);
            if (StringUtils.isEmpty(role.getcRoleName())) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE, "角色名称为空！！！", null);
            }
            if (iroleService.selectByName(role) != null) {
                return AjaxResponse.reponseBody(Constant.CHECK_CODE, "角色名称已存在", null);
            }
            iroleService.updateRole(role);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, null);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }
    
    
    @ApiOperation(value = "禁用或者启用", notes = "")
    @ResponseBody
    @RequestMapping(value = "/forbidden", method = RequestMethod.PUT)
    public AjaxResponse forbidden(@RequestParam("roleid") int roleid,@RequestParam("state") Short state) {
        try {
            Role role = new Role();
            role.setcRoleId(roleid);
            role.setState(state);
            iroleService.Enabledisable(role);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, null);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }
    
    
    @ApiOperation(value = "删除角色", notes = "")
    @ResponseBody
    @RequestMapping(value = "/deleteRole", method = RequestMethod.PUT)
    public AjaxResponse deleteUser(@RequestParam("roleid") int roleid) {
        try {
        	User user = new User();
        	user.setRoleId(roleid);
        	List<User> list = userService.selectByCondition(user);
        	if(list.size() > 0) {
        		 return AjaxResponse.reponseBody(Constant.CHECK_CODE, "角色与用户表关联！！！", null);
        	}
            iroleService.deleteRole(roleid);
            return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, null);
        } catch (Exception e) {
            LOGGER.error("", e);
            return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
        }
    }
    
    @ApiOperation(value = "查询角色详情", notes = "")
    @OperationLog(operationModule = Constant.MODULE_ROLE, operationFunction = "删除角色")
    @ResponseBody
    @RequestMapping(value = "/selectRoleList", method = RequestMethod.PUT)
    public AjaxResponse selectByRoleIdList(@RequestParam("roleid") int roleid) {
    	try {
    		List<RolePermission> list = iroleService.selectByRoleIdList(roleid);
    		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, list);
    	} catch (Exception e) {
    		LOGGER.error("", e);
    		return AjaxResponse.reponseBody(Constant.FAIL_CODE, Constant.FAIL_MSG, null);
    	}
    }
}
